<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <!-- el-row / el-col 属于布局容器 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px;"
        >
          <el-col :span="20">
            <svg-icon icon-class="gs" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 树形组织 -->
        <!-- default-expand-all: 默认展开所有子节点 -->
        <el-tree :data="list" :default-expand-all="true">
          <template v-slot="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px;width:100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <!-- 传入空的 id 就表示一级部门 -->
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑子部门</el-dropdown-item>
                        <!-- 优化: 如果没有子部门就显示删除按钮 -->
                        <el-dropdown-item v-if="!data.children.length" @click.native="hDel(data.id)">删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit?'编辑':'添加'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <AddOrEdit v-if="showDialog" :id="curId" :origin-list="originList" :is-edit="isEdit" @success="hSuccess" @close="hClose" />
    </el-dialog>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddOrEdit from './deptDialog.vue'
export default {
  components: {
    AddOrEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false,
      originList: []

    }
  },
  async created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res)
        res.data.depts.shift()

        this.originList = res.data.depts.map(({ id, pid, code, name }) => {
          return { id, pid, code, name }
        })
        this.list = tranListToTreeData(res.data.depts)
      } catch (err) {
        console.log(err)
      }
    },
    hAdd(id) {
      // 打开对话框
      this.showDialog = true
      // console.log(id)
      // 把id传给子组件，在子组件中去获取详情
      this.curId = id
      // 关闭对话框
      this.isEdit = false
    },
    hEdit(id) {
      // 打开对话框
      this.showDialog = true
      // 把id传给子组件，在子组件中去获取详情
      this.curId = id
      // 3. 传递一个标识给子组件，告诉它我们现在是编辑状态
      this.isEdit = true
    },
    hDel(id) {
      this.$confirm('此操作将永久删除此部门,是否继续?', '提示', {
        // confirmButtonText: '确定',
        // cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          try {
            const res = await delDepartment(id)
            // console.log(res)
            this.$message.success(res.message)
            this.loadDepartments()
          } catch (e) {
            this.$message.error(e.message)
          }
        })
        .catch(e => {})
    },
    hSuccess() {
      // 收到子组件通知的成功, 报喜
      // 关闭 dialog
      this.showDialog = false
      // 获取最新的数据
      this.loadDepartments()
    },
    hClose() {
      this.showDialog = false
    }
  }
}
</script>
